home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Guided Tour of Multimedia (Second Edition)
/
The Guided Tour of Multimedia (Second Edition).iso
/
trials
/
director
/
evalcopy
/
director.z
/
COMMPORT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-06-02
|
30KB
|
1,003 lines
/*****************************************************************************
Macromedia CommPort XObject
(c) Copyright 1993 Macromedia
All Rights Reserved
*****************************************************************************/
#include <windows.h>
#include "XObject.h"
/*
--------------------------------------------------------------------------
Function Prototypes:
--------------------------------------------------------------------------
*/
short __far __pascal __export LibMain(
HANDLE hndInstance, WORD suDataSeg, WORD suHeapSize, LPBYTE pCmdLine);
//DJ20jul93int _export _far _pascal WEP(int nParameter);
long __far __pascal __export _CommPort_mNew(
LxMemHandle hPortName, long lRecvBufSize, long lXmitBufSize,
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mDispose(
LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mName(
LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mPortName(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mStatus(
LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mError(
long lErrorCode, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetup(
long lBaudRate, long lStopBits, long lParity,
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetBaudRate(
long lBaudRate, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mBaudRate(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetDataBits(
long lDataBits, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mDataBits(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetStopBits(
long lStopBits, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mStopBits(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetParity(
LxMemHandle hParity, LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mParity(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mSetFlowCtl(
LxMemHandle hFlow, LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mFlowCtl(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mWriteChar(
long lCharNum, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mWriteString(
LxMemHandle hData, LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mWriteCount(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mWriteFlush(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mReadChar(
LxProcTablePtr xtbl, LxMemHandle hComm);
LxMemHandle __far __pascal __export _CommPort_mReadString(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mReadCount(
LxProcTablePtr xtbl, LxMemHandle hComm);
long __far __pascal __export _CommPort_mReadFlush(
LxProcTablePtr xtbl, LxMemHandle hComm);
/*
--------------------------------------------------------------------------
Constants:
--------------------------------------------------------------------------
*/
#define COMMPORT_FLOW_XONXOFF 1
#define COMMPORT_FLOW_HARDWARE 2
#define COMMPORT_FLOW_NONE 3
#define COMM_SUCCESS 0
/*
--------------------------------------------------------------------------
Types:
--------------------------------------------------------------------------
*/
typedef struct tagCommPort
{
LxXObjHeader head; // Required header
short sCommID; // COM port ID
LxMemHandle hPortName; // COM port name (string handle)
char bFlowCtl; // COM port flow control setting
} CommPort, FAR * pCommPort;
/*
------------------------------------------------------------------------
Variables:
------------------------------------------------------------------------
*/
long lCommPortError = COMM_SUCCESS;
HANDLE hInst;
/*
------------------------------------------------------------------------
Macros:
------------------------------------------------------------------------
*/
/*
------------------------------------------------------------------------
FUNCTION || LibMain()
Windows Entry Procedure for DLLs. Called by LibEntry routine.
------------------------------------------------------------------------
*/
short __far __pascal __export LibMain(
HANDLE hndInstance, WORD suDataSeg, WORD suHeapSize, LPBYTE pCmdLine)
{
if (suHeapSize != 0)
{
if (! LocalInit( (UINT)suDataSeg, (UINT)NULL, (UINT)suHeapSize))
{
return (0);
}
}
hInst = hndInstance;
return (1);
}
//DJ20jul93/*
//DJ20jul93--------------------------------------------------------------------------
//DJ20jul93FUNCTION || WEP()
//DJ20jul93
//DJ20jul93Windows Exit Procedure for DLLs.
//DJ20jul93--------------------------------------------------------------------------
//DJ20jul93*/
//DJ20jul93int _export _far _pascal WEP(int nParameter)
//DJ20jul93{
//DJ20jul93 return 1;
//DJ20jul93}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mNew
Creates a new instance of a CommPort XObject. Opens COM port and
initializes instance variables.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mNew(
LxMemHandle hPortName, long lRecvBufSize, long lXmitBufSize,
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LPSTR pstrName;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pstrName = xtbl->mem_Lock(hPortName);
if ((hComm = xtbl->mem_SetSize(hComm, (long)sizeof(CommPort))) != NULL)
{
pComm = xtbl->mem_Lock(hComm);
pComm->sCommID = OpenComm(pstrName, (WORD) lRecvBufSize,
(WORD) lXmitBufSize);
if (pComm->sCommID < 0)
{
lCommPortError = pComm->sCommID;
}
else
{
pComm->hPortName = xtbl->string_New(pstrName);
// Initialize flow control to "None"
pComm->bFlowCtl = COMMPORT_FLOW_NONE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fDtrflow = FALSE;
dcb.fRtsflow = FALSE;
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
}
xtbl->mem_Unlock(hComm);
}
else
{
lCommPortError = IE_MEMORY;
}
xtbl->mem_Unlock(hPortName);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mDispose
Dispose of XObject instance.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mDispose(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
if (CloseComm(pComm->sCommID) <= 0)
{
/* don't know what error code to use here */
}
if (pComm->hPortName != NULL)
{
xtbl->mem_Dispose(pComm->hPortName);
}
xtbl->mem_Unlock(hComm);
xtbl->xobj_Dispose(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mName
Returns name of XObject (e.g. "CommPort")
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mName(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
lCommPortError = COMM_SUCCESS;
return (xtbl->string_New("CommPort"));
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mPortName
Returns name of opened COM port (e.g., "COM1")
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mPortName(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LxMemHandle hName;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
hName = xtbl->mem_Clone(pComm->hPortName);
xtbl->mem_Unlock(hComm);
return (hName);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mStatus
Returns numberic status of last method call.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mStatus(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mError
Returns error string, given error code (from mStatus method).
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mError(
long lErrorCode, LxProcTablePtr xtbl, LxMemHandle hComm)
{
LPSTR pstrMsg;
switch ((short) lErrorCode)
{
case COMM_SUCCESS:
pstrMsg = "";
break;
case CE_RXOVER:
pstrMsg = "Receive Queue overflow";
break;
case CE_OVERRUN:
pstrMsg = "Receive Overrun Error";
break;
case CE_RXPARITY:
pstrMsg = "Receive Parity Error";
break;
case CE_FRAME:
pstrMsg = "Receive Framing error";
break;
case CE_BREAK:
pstrMsg = "Break Detected";
break;
case CE_CTSTO:
pstrMsg = "CTS Timeout";
break;
case CE_DSRTO:
pstrMsg = "DSR Timeout";
break;
case CE_RLSDTO:
pstrMsg = "RLSD Timeout";
break;
case CE_TXFULL:
pstrMsg = "TX Queue is full";
break;
case CE_PTO:
pstrMsg = "LPTx Timeout";
break;
case CE_IOE:
pstrMsg = "LPTx I/O Error";
break;
case CE_DNS:
pstrMsg = "LPTx Device not selected";
break;
case CE_OOP:
pstrMsg = "LPTx Out-Of-Paper";
break;
case CE_MODE:
pstrMsg = "Requested mode unsupported";
break;
case IE_BADID:
pstrMsg = "Invalid or unsupported id";
break;
case IE_OPEN:
pstrMsg = "Device Already Open";
break;
case IE_NOPEN:
pstrMsg = "Device Not Open";
break;
case IE_MEMORY:
pstrMsg = "Unable to allocate memory";
break;
case IE_DEFAULT:
pstrMsg = "Error in default parameters";
break;
case IE_HARDWARE:
pstrMsg = "Hardware Not Present";
break;
case IE_BYTESIZE:
pstrMsg = "Illegal Byte Size";
break;
case IE_BAUDRATE:
pstrMsg = "Unsupported BaudRate";
break;
default:
pstrMsg = "Unknown error";
break;
}
return (xtbl->string_New(pstrMsg));
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetup
Sets the COM port baud rate, stop bits, and parity. Compatible with
Mac SerialPort XObject calling conventions.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetup(
long lBaudRate, long lStopBits, long lParity,
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
dcb.BaudRate = (WORD) lBaudRate;
dcb.ByteSize = 8;
// Set Stop Bits:
if (lStopBits == 10)
{
dcb.StopBits = ONESTOPBIT;
}
else if (lStopBits == 15)
{
dcb.StopBits = ONE5STOPBITS;
}
else if (lStopBits == 20)
{
dcb.StopBits = TWOSTOPBITS;
}
// Set Parity Value:
if (lParity == 0)
{
dcb.Parity = NOPARITY;
}
else if (lParity == 1)
{
dcb.Parity = ODDPARITY;
}
else if (lParity == 2)
{
dcb.Parity = EVENPARITY;
}
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetBaudRate
Sets the COM port baud rate.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetBaudRate(
long lBaudRate, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
dcb.BaudRate = (WORD) lBaudRate;
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mBaudRate
Returns the COM ports baud rate.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mBaudRate(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
long lBaudRate;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
lBaudRate = dcb.BaudRate;
xtbl->mem_Unlock(hComm);
return (lBaudRate);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetDataBits
Sets the number of data bits (in lDataBits):
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetDataBits(
long lDataBits, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
dcb.ByteSize = (BYTE) lDataBits;
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mDataBits
Returns the number of data bits.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mDataBits(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
long lDataBits;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
lDataBits = dcb.ByteSize;
xtbl->mem_Unlock(hComm);
return (lDataBits);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetStopBits
Sets the number of stop bits (in lStopBits):
10 -> 1 Stop bits
15 -> 1.5 Stop bits
20 -> 2 Stop bits
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetStopBits(
long lStopBits, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
if (lStopBits == 10)
{
dcb.StopBits = ONESTOPBIT;
}
else if (lStopBits == 15)
{
dcb.StopBits = ONE5STOPBITS;
}
else if (lStopBits == 20)
{
dcb.StopBits = TWOSTOPBITS;
}
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mStopBits
Returns the number of stop bits. (See mSetStopBits for definition of
return values.)
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mStopBits(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
long lStopBits;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
if (dcb.StopBits == ONESTOPBIT)
{
lStopBits = 10;
}
else if (dcb.StopBits == ONE5STOPBITS)
{
lStopBits = 15;
}
else if (dcb.StopBits == TWOSTOPBITS)
{
lStopBits = 20;
}
else
{
lStopBits = 0;
}
xtbl->mem_Unlock(hComm);
return (lStopBits);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetParity
Sets the COM port parity (in string hParity):
"Odd" Sets odd parity
"Even" Sets even parity
"Mark" Sets mark parity
"Space" Sets space parity
"None" Sets no parity
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetParity(
LxMemHandle hParity, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LPSTR pstrParity;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
pstrParity = xtbl->mem_Lock(hParity);
GetCommState(pComm->sCommID, &dcb);
if (lstrcmpi(pstrParity, "Even") == 0)
{
dcb.Parity = EVENPARITY;
}
else if (lstrcmpi(pstrParity, "Odd") == 0)
{
dcb.Parity = ODDPARITY;
}
else if (lstrcmpi(pstrParity, "Mark") == 0)
{
dcb.Parity = MARKPARITY;
}
else if (lstrcmpi(pstrParity, "Space") == 0) {
dcb.Parity = SPACEPARITY;
}
else if (lstrcmpi(pstrParity, "None") == 0)
{
dcb.Parity = NOPARITY;
}
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hParity);
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mParity
Returns the current COM port parity (as a string). See mSetParity for
possible return values.
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mParity(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LPSTR pstrParity;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
if (dcb.Parity == EVENPARITY)
{
pstrParity = "Even";
}
else if (dcb.Parity == ODDPARITY)
{
pstrParity = "Odd";
}
else if (dcb.Parity == MARKPARITY)
{
pstrParity = "Mark";
}
else if (dcb.Parity == SPACEPARITY)
{
pstrParity = "Space";
}
else if (dcb.Parity == NOPARITY)
{
pstrParity = "None";
}
else
{
pstrParity = "";
}
xtbl->mem_Unlock(hComm);
return (xtbl->string_New(pstrParity));
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mSetFlowCtl
Set flow control for COM Port (in string hFlow):
"XonXoff" Sets xon/xoff protocal
"Hardware" Sets CTS/RTS hardware protocal
"None" Sets no flow control
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mSetFlowCtl(
LxMemHandle hFlow, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LPSTR pstrFlow;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
pstrFlow = xtbl->mem_Lock(hFlow);
GetCommState(pComm->sCommID, &dcb);
if (lstrcmpi(pstrFlow, "XonXoff") == 0)
{
pComm->bFlowCtl = COMMPORT_FLOW_XONXOFF;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = TRUE;
dcb.fInX = TRUE;
dcb.fDtrflow = FALSE;
dcb.fRtsflow = FALSE;
}
else if (lstrcmpi(pstrFlow, "Hardware") == 0)
{
pComm->bFlowCtl = COMMPORT_FLOW_HARDWARE;
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fDtrflow = TRUE;
dcb.fRtsflow = TRUE;
}
else if (lstrcmpi(pstrFlow, "None") == 0)
{
pComm->bFlowCtl = COMMPORT_FLOW_NONE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fDtrflow = FALSE;
dcb.fRtsflow = FALSE;
}
if (SetCommState(&dcb) < 0)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hFlow);
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mFlowCtl
Returns (as a string) the current flow control setting. See mSetFlowCtl
for possible return values.
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mFlowCtl(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
LPSTR pstrFlow;
DCB dcb;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommState(pComm->sCommID, &dcb);
if (pComm->bFlowCtl == COMMPORT_FLOW_XONXOFF)
{
pstrFlow = "XonXoff";
}
else if (pComm->bFlowCtl == COMMPORT_FLOW_HARDWARE)
{
pstrFlow = "Hardware";
}
else if (pComm->bFlowCtl == COMMPORT_FLOW_NONE)
{
pstrFlow = "None";
}
else
{
pstrFlow = "";
}
xtbl->mem_Unlock(hComm);
return (xtbl->string_New(pstrFlow));
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mWriteChar
Write a single character (given as an ASCII code value) to the COM port.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mWriteChar(
long lCharNum, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
BYTE strChar;
pComm = xtbl->mem_Lock(hComm);
strChar = (BYTE) lCharNum;
if (WriteComm(pComm->sCommID, &strChar, 1) != 1)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mWriteString
Write a null-terminated string to the COM port.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mWriteString(
LxMemHandle hData, LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
short sLen;
LPSTR pstrData;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
pstrData = xtbl->mem_Lock(hData);
sLen = lstrlen(pstrData);
if (WriteComm(pComm->sCommID, pstrData, sLen) != sLen)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hData);
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mWriteCount
Return the pending write count for the last transmission.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mWriteCount(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
long lCount;
COMSTAT stat;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommError(pComm->sCommID, &stat);
lCount = stat.cbOutQue;
xtbl->mem_Unlock(hComm);
return (lCount);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mWriteFlush
Flush any pending writes to the COM port.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mWriteFlush(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
FlushComm(pComm->sCommID, 0); /* 0 = transmit buffer */
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mReadChar
Read and return a single character (as an ASCII code value) from the COM port.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mReadChar(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
BYTE strChar;
long lCharNum;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
if (ReadComm(pComm->sCommID, &strChar, 1) <= 0)
{
/* zero count from ReadComm may or may not be an error */
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
if (lCommPortError == COMM_SUCCESS)
{
lCharNum = strChar;
}
else
{
lCharNum = -1;
}
xtbl->mem_Unlock(hComm);
return (lCharNum);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mReadString
Read and return a null-terminated string from the COM port.
--------------------------------------------------------------------------
*/
LxMemHandle __far __pascal __export _CommPort_mReadString(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
COMSTAT stat;
short sLen;
LxMemHandle hData;
LPSTR pbData;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommError(pComm->sCommID, &stat);
sLen = stat.cbInQue;
if (sLen == 0)
{
hData = xtbl->mem_New(1, TRUE);
}
else
{
/* should be able to read at least sLen bytes */
hData = xtbl->mem_New((long)sLen + 1, TRUE);
if (hData != NULL)
{
pbData = xtbl->mem_Lock(hData);
if (ReadComm(pComm->sCommID, pbData, sLen) != sLen)
{
lCommPortError = GetCommError(pComm->sCommID, NULL);
}
xtbl->mem_Unlock(hData);/* was cleared to NULL when allocated */
}
else
{
lCommPortError = IE_MEMORY;
}
}
xtbl->mem_Unlock(hComm);
return (hData);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mReadCount
Return a count of characters pending a read operation.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mReadCount(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
long lCount;
COMSTAT stat;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
GetCommError(pComm->sCommID, &stat);
lCount = stat.cbInQue;
xtbl->mem_Unlock(hComm);
return (lCount);
}
/*
--------------------------------------------------------------------------
FUNCTION || _CommPort_mReadFlush
Flush any pending input from the COM port.
--------------------------------------------------------------------------
*/
long __far __pascal __export _CommPort_mReadFlush(
LxProcTablePtr xtbl, LxMemHandle hComm)
{
pCommPort pComm;
lCommPortError = COMM_SUCCESS;
pComm = xtbl->mem_Lock(hComm);
FlushComm(pComm->sCommID, 1); /* 1 = receive buffer */
xtbl->mem_Unlock(hComm);
return (lCommPortError);
}